package com.ruoyi.personnel.salary.piece.service;

import com.ruoyi.personnel.salary.piece.domain.PieceCond;
import com.ruoyi.personnel.salary.piece.domain.PieceDomain;
import com.ruoyi.personnel.salary.piece.domain.PieceDto;
import com.ruoyi.personnel.salary.piece.mapper.PieceMapper;
import com.ruoyi.produce.process.receiving.domain.ReceivingReportDto;
import com.ruoyi.produce.process.receiving.mapper.ReceivingReportMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 计件工资
 * 业务处理层
 */
@Service
public class PieceServiceImpl implements IPieceService{
    @Autowired
    private PieceMapper pieceMapper;
    @Autowired
    private ReceivingReportMapper receivingReportMapper; //关联验收单

    /**
     * 根据搜索条件所有计件工资记录
     * @param cond 搜索条件
     * @return
     */
    @Override
    public List<PieceDto> selectPieceList(PieceCond cond) {
        List<PieceDto> pieceDtos = pieceMapper.selectPieceList(cond);
        for (int i = 0; i < pieceDtos.size(); i++) {
            // 根据验收单号获取验收单数据
            ReceivingReportDto receivingReport = receivingReportMapper.selectReceivingReportById(pieceDtos.get(i).getReceivingReportId());
            // 计算合格品金额，合格品数量*合格品单价
            Double qualifiedPrice = pieceDtos.get(i).getQualifiedNumber() * pieceDtos.get(i).getQualifiedUnitPrice();
            pieceDtos.get(i).setQualifiedPrice(qualifiedPrice);
            // 计算扣报废品金额，报废品数量*报废品单价
            Double unqualifiedPrice = pieceDtos.get(i).getUnqualifiedNumber() * pieceDtos.get(i).getUnqualifiedUnitPrice();
            pieceDtos.get(i).setUnqualifiedPrice(unqualifiedPrice);
            // 计算计件工资
            pieceDtos.get(i).setPieceRate(qualifiedPrice - unqualifiedPrice);
            // 通过验收单获取相关产品信息，将其赋值给计件工资的相关产品信息
            pieceDtos.get(i).setProduceId(receivingReport.getProduceId());
            pieceDtos.get(i).setProduceName(receivingReport.getProduceName());
            pieceDtos.get(i).setBaseCategoryName(receivingReport.getBaseCategoryName());
            pieceDtos.get(i).setBaseUnit(receivingReport.getBaseUnit());
            pieceDtos.get(i).setProduceSpecs(receivingReport.getProduceSpecs());
        }
        return pieceDtos;
    }

    /**
     * 根据员工编号搜索具体的计件工资记录
     * @param staffId 员工编号
     * @return
     */
    @Override
    public PieceDto selectPieceById(String staffId) {
        return pieceMapper.selectPieceById(staffId);
    }

    /**
     * 新增计件工资记录
     * @param piece
     * @return
     */
    @Override
    public int insertPiece(PieceDomain piece) {
        piece.setQualifiedPrice(piece.getQualifiedNumber() * piece.getQualifiedUnitPrice());
        piece.setUnqualifiedPrice(piece.getUnqualifiedNumber() * piece.getUnqualifiedUnitPrice());
        System.out.println("domain" + piece);
        return pieceMapper.insertPiece(piece);
    }

    /**
     * 修改计件工资
     * @param piece
     * @return
     */
    @Override
    public int updatePiece(PieceDomain piece) {
        return pieceMapper.updatePiece(piece);
    }

    /**
     * 删除计件工资记录
     * 仅修改删除标志
     * @param piece
     * @return
     */
    @Override
    public int updatePieceDeleteFlag(PieceDomain piece) {
        return pieceMapper.updatePieceDeleteFlag(piece);
    }
}
